package top.xtcoder.jdcbase.base.exception;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import top.xtcoder.jdcbase.base.core.Resp;

@RestControllerAdvice
public class AuthExceptionResolver {
	private final Logger log = LogManager.getLogger(AuthExceptionResolver.class);
	
	/**
	 * 捕获未登录操作异常
	 * @param e
	 * @param req
	 * @return
	 */
	@ExceptionHandler(value=NotLoginException.class)
	public Object handleException(NotLoginException e, HttpServletRequest req) {
		e.printStackTrace();
		return Resp.NEW(Resp.NOT_LOGIN, "未登录", e.getMessage());
	}
	
	/**
	 * 捕获无权限操作异常
	 * @param e
	 * @param req
	 * @return
	 */
	@ExceptionHandler(value=NotPermissionException.class)
	public Object handleException(NotPermissionException e, HttpServletRequest req) {
		return Resp.NEW(Resp.NOT_PERM, e.getMessage(), e.getMessage());
	}
	
	/**
	 * 捕获无角色操作异常
	 * @param e
	 * @param req
	 * @return
	 */
	@ExceptionHandler(value=NotRoleException.class)
	public Object handleException(NotRoleException e, HttpServletRequest req) {
		e.printStackTrace();
		return Resp.NEW(Resp.NOT_PERM, e.getMessage(), e.getMessage());
	}
}
